//
//  RecommendContainerView.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/10.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "RecommendContainerView.h"

@implementation RecommendContainerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame images:(NSArray *)images
{
    self = [self initWithFrame:frame];
    
    if (self) {
        
        self.contentSize = CGSizeMake(frame.size.width * [images count], frame.size.height);
        
        self.pagingEnabled = YES;
        self.delegate = self;
        
        self.showsHorizontalScrollIndicator = NO;
        
        [self setContentOffset:CGPointMake(frame.size.width, 0) animated:YES];
        
        //图片个数 记录数组中有多少张图
        self.imagesCount = [images count];
        
        for (int i = 0; i < [images count]; i++) {
            
            
            //循环创建imageView, 把图片添加到imageView
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * frame.size.width, 5, frame.size.width, frame.size.height)];
            self.imageView.image = [images objectAtIndex:i];


            self.imageView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;
            [self.imageView addGestureRecognizer:tap];
            self.imageView.tag = 1000 + i;
            [self addSubview:self.imageView];
            
            
            
            
        }
        
        
    }
    return self;
    
}




//轻拍手势
- (void)tapAction:(UITapGestureRecognizer *)tag
{
    NSInteger index = self.contentOffset.x / self.frame.size.width;

    self.currentIndex = index;
    
    [self.pushDelegate pushAction:index];

}



//减速结束后调用
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    
//    for (UIView *view in self.subviews) {
//        if ([view isKindOfClass:[UIScrollView class]]) {
//            
//            UIScrollView *scrollView = (UIScrollView *)view;
//            scrollView.zoomScale = 1.0;
//        }
//    }
//}


//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
////    if (self.contentOffset.x == 0) {
////        self.contentOffset = CGPointMake(self.frame.size.width * 4, 0);
////    }
////    if (self.contentOffset.x == self.frame.size.width * 5) {
////        
////        self.contentOffset = CGPointMake(self.frame.size.width, 0);
////    }
//    
////    
//    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
//    
//    
//    
//    [self.containDelegate changePageIndex:index];
//    [self scrollViewDidScroll:scrollView];
//
//}


//无限滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{


    if (self.contentOffset.x == 0) {
        self.contentOffset = CGPointMake(self.frame.size.width * 4, 0);
    }
    if (self.contentOffset.x == self.frame.size.width * 5) {

        self.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    

    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
 
    
    
    [self.containDelegate changePageIndex:index];
}
















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
